package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Controls.FenQiList;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.GognHuoShang.InsertGongHuoShangActivity;
import com.jackie.tongzhuang.JinHuoDan.JinHuoDanFenQiActivity;
import com.jackie.tongzhuang.Models.FenQi;
import com.jackie.tongzhuang.Models.YinHangKa;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView bqrqtext,bqhketext,xqrqtext,xqhketext,bqed1text,yyedtext,kyedtext,mxtext;
    private LinearLayout bqmxlay,xqmxlay,mxlay;
    private LinearLayout ckfqlay,ghslay,jhlay,szlay;
    private Button hkbutton;
    private TitleBar titlebar;
    private DbClass dbClass = new DbClass(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        //判断是否有分期账户
        long yhk_count = dbClass.getTableCount("YinHangKa");
        if(yhk_count == 0){
            Intent intent = new Intent(MainActivity.this,InsertYinHangKaActivity.class);
            startActivity(intent);
        }
        //判断是否有供货商
        long ghs_count = dbClass.getTableCount("GongHuoShang");
        if(ghs_count == 0){
            Intent intent = new Intent(MainActivity.this, InsertGongHuoShangActivity.class);
            startActivity(intent);
        }

        titlebar.setText("分  期");
        titlebar.setTextSize(18);
        titlebar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        titlebar.setHideSubmit(true);
        titlebar.setHideBack(true);




    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取本期日期和下一期日期
        String bqrq = BaseClass.getNextFenQiData(1);
        bqrqtext.setText(bqrq);

        double bqje = dbClass.getXiaQiYingHuan(bqrq);
        String bqjestr= "";
        if(bqje != 0) {
            bqed1text.setVisibility(View.VISIBLE);
            bqjestr = BaseClass.getDecimalToStr(2, bqje);
            hkbutton.setVisibility(View.VISIBLE);
        }else{
            bqed1text.setVisibility(View.GONE);
            bqjestr = "本期账单已还清";
            hkbutton.setVisibility(View.GONE);
        }
        bqhketext.setText(bqjestr);

        String xqrq = BaseClass.getNextFenQiData(2);
        xqrqtext.setText(xqrq);

        double xqje = dbClass.getXiaQiYingHuan(xqrq);
        String xqjestr= "";
        if(xqje != 0) {
            xqjestr = BaseClass.getDecimalToStr(2, xqje);
        }else{
            xqjestr = "0.00";
        }
        xqhketext.setText(xqjestr);

        //获取可用额度
        YinHangKa yhk = dbClass.getYinHangKa(1);
        double ed = yhk.geteDu();
        double kyed = yhk.getKeYongEDu();
        double yyed = ed - kyed;
        kyedtext.setText(BaseClass.getDecimalToStr(2,kyed));
        yyedtext.setText(BaseClass.getDecimalToStr(2,yyed));


        mxlay.removeAllViews();
        mxtext.setText("本期账单明细");
        showMingXi(1);



    }

    private void initView(){
        titlebar = findViewById(R.id.main_title);
        bqrqtext = findViewById(R.id.fq_main_bqrq);
        xqrqtext = findViewById(R.id.fq_main_xqrq);
        bqhketext = findViewById(R.id.fq_main_bqed);
        xqhketext = findViewById(R.id.fq_main_xqed);
        bqmxlay = findViewById(R.id.fq_main_bqmx);
        xqmxlay = findViewById(R.id.fq_main_xqmx);
        yyedtext = findViewById(R.id.fq_main_yyed);
        kyedtext = findViewById(R.id.fq_main_kyed);
        hkbutton = findViewById(R.id.fq_main_yjhk);
        bqed1text = findViewById(R.id.fq_main_bqed1);
        mxlay = findViewById(R.id.fq_main_mxlay);
        mxtext = findViewById(R.id.fq_main_mxtext);
        ckfqlay = findViewById(R.id.fq_main_ckfqlay);
        ghslay = findViewById(R.id.fq_main_ghslay);
        jhlay = findViewById(R.id.fq_main_jhlay);
        szlay = findViewById(R.id.fq_main_szlay);
        hkbutton.setOnClickListener(this);
        bqmxlay.setOnClickListener(this);
        xqmxlay.setOnClickListener(this);
        ckfqlay.setOnClickListener(this);
        ghslay.setOnClickListener(this);
        jhlay.setOnClickListener(this);
        szlay.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fq_main_yjhk:
                Intent intent = new Intent(MainActivity.this, HuanKuanSelectActivity.class);
                intent.putExtra("hke",bqhketext.getText());
                startActivity(intent);
                break;
            case R.id.fq_main_bqmx:
                mxlay.removeAllViews();
                mxtext.setText("本期账单明细");
                showMingXi(1);
                break;
            case R.id.fq_main_xqmx:
                mxlay.removeAllViews();
                mxtext.setText("下期账单明细");
                showMingXi(2);
                break;
            case R.id.fq_main_ckfqlay:
                Intent ckfqintent = new Intent(MainActivity.this, JinHuoDanFenQiActivity.class);
                startActivity(ckfqintent);
                break;
            case R.id.fq_main_ghslay:
                Intent ghsintent = new Intent(MainActivity.this, InsertGongHuoShangActivity.class);
                startActivity(ghsintent);
                break;
            case R.id.fq_main_jhlay:
                Intent jhIntent = new Intent(MainActivity.this, InsertJinHuoDanActivity.class);
                startActivity(jhIntent);
                break;
        }
    }

    private void showMingXi(int i){
        List<FenQi> fqs1 = dbClass.getFenQiByData(BaseClass.getNextFenQiData(i));
        for(FenQi fq:fqs1){
            FenQiList fql = new FenQiList(MainActivity.this,null);
            fql.setId(fq.getId());
            fql.setRiQi(fq.getChangHuanRiQi());
            fql.setJinE(String.valueOf(fq.getFenQiJinE()));
            fql.setShowcheck(false);
            fql.setBeiZhu(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu()+" 期");
            mxlay.addView(fql);
        }
    }

}